package com.hirel.web.common;

import javax.validation.Valid;

import org.hibernate.mapping.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hirel.domain.common.UserDetailsDTO;
import com.hirel.service.common.UserDetailsService;

@Controller

public class UserDetailsController {

	@Autowired(required=true)
	private UserDetailsService userDetailsService;

	@RequestMapping("loginUser.htm")
	public ModelAndView userLogin() {
		ModelAndView mav = new ModelAndView("/common/loginUserForm");

		mav.addObject("registerUser", new UserDetailsDTO());

		return mav;

	}
	@RequestMapping(value="loginUserForm.htm" , method = RequestMethod.POST)
	public String processForm(@Valid UserDetailsDTO userDetailsDTO, BindingResult result,
	Map model) {
	String userName = "Admin";
	String password = "root";
	if (result.hasErrors())
	return "loginform";
	return password;

	}
	
	

	@RequestMapping(value = "addUserDetails.htm")
	public ModelAndView userRegister(
			@ModelAttribute("registerForm") UserDetailsDTO userDetailsDTO,
			BindingResult result) {

		ModelAndView mav = new ModelAndView();
		if (result.hasErrors()) {
			mav.setViewName("addUserDetails");
			mav.addObject("registerForm", userDetailsDTO);
			System.out.println("Errror has been  occurred");
			return mav;

		}

	//	userDetailsService.addUser(userDetailsDTO);
		mav.setViewName("registerSuccessPage.htm");
		mav.addObject("userDetails", userDetailsDTO);
		mav.addObject("status", "Student Form successfully");
		return mav;
	}

}
